package my.edu.common.config;

import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;

public class SecurityManagerAwareRunnable implements Runnable {

    private final Runnable task;

    public SecurityManagerAwareRunnable(Runnable task) {
        this.task = task;
    }

    @Override
    public void run() {
        try {
            // 获取 SecurityManager
            SecurityManager securityManager = SecurityUtils.getSecurityManager();
            // 绑定 SecurityManager 到当前线程
            ThreadContext.bind(securityManager);

            // 执行原始任务
            task.run();
        } finally {
            // 确保任务完成后解除绑定
            ThreadContext.unbindSecurityManager();
        }
    }
}
